package com.bright.service.serviceimpl;

import com.bright.dao.CommentDao;
import com.bright.pojo.Blog;
import com.bright.pojo.Comment;
import com.bright.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    private CommentDao commentDao;

    @Override
    public List<Comment> listComment(long blogid) {
        Sort sort1 = Sort.by(Sort.Direction.DESC,"createTime");
        return commentDao.getByParentCommentNullAndBlogId(blogid,sort1);
    }

    @Transactional
    @Override
    public Comment saveCommnet(Comment comment) {
        long parentCommentid = comment.getParentComment().getId();
        if (parentCommentid!=-1){
            comment.setParentComment(commentDao.getOne(parentCommentid));
        }else {
            comment.setParentComment(null);
        }
        comment.setCreateTime(new Date());
        return commentDao.save(comment);
    }

    @Override
    public List<Comment> getreplayComments(long blogid) {
        Sort sort = Sort.by(Sort.Direction.DESC,"createTime");
        return commentDao.getByParentCommentNotNullAndBlogId(blogid,sort);
    }

}
